package com.css.dzj.zfjg.modules.jcdx.controller;

import com.css.aq.server.service.BaseService;
import com.css.dzj.zfjg.common.util.R;
import com.css.dzj.zfjg.modules.jcdx.entity.JgLhjgJcdxEntity;
import com.css.dzj.zfjg.modules.jcdx.service.JgLhjgJcdxService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

/**
 * 联合监管检查对象表Controller
 *
 * @author liyuan
 */
@RestController
@RequestMapping("lhjg/jglhjgjcdx")
public class JgLhjgJcdxController {
    @Autowired
    private JgLhjgJcdxService jcdxService;

    @Autowired
    private BaseService baseService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam(required = false) String objectname,
                  @RequestParam Integer pageSize, @RequestParam Integer pageNo) {
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<JgLhjgJcdxEntity> page = jcdxService.queryPage(objectname, pageRequest);
            baseService.logSuccess(3, "分页查询联合监管检查对象信息成功", "2");
            return R.ok().put("page", page);
        } catch (Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "分页查询联合监管检查对象信息失败", "2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{taskno}")
    public R info(@PathVariable("taskno") String taskno) {
        try {
            JgLhjgJcdxEntity jgLhjgJcdx = jcdxService.getById(taskno);
            baseService.logSuccess(3, "通过id获取联合监管检查对象信息成功", "2");
            return R.ok().put("jgLhjgJcdx", jgLhjgJcdx);
        } catch (Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "通过id获取联合监管检查对象信息失败", "2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody JgLhjgJcdxEntity jgLhjgJcdx) {
        try {
            jcdxService.save(jgLhjgJcdx);
            baseService.logSuccess(3, "联合监管检查对象信息添加成功", "2");
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "联合监管检查对象信息添加失败", "2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody JgLhjgJcdxEntity jgLhjgJcdx) {
        try {
            jcdxService.updateById(jgLhjgJcdx);
            baseService.logSuccess(3, "联合监管检查对象信息修改成功", "2");
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "联合监管检查对象信息修改失败", "2");
            return R.error(e.getMessage());
        }
    }
}
